package com.it.od.manual;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author: liminghui
 * @date: 2024/3/14 22:34
 * @version: 1.0
 * @description: 1.岛屿周长
 * 2.字符串压缩
 * 3,二叉树遍历
 *       5
 *     3      7
 *   2    4   6  8
 * 10 11
 */
public class Tree {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(8);
        root.left.left.left = new TreeNode(10);
        root.left.left.right = new TreeNode(11);
        List<List<Integer>> res = breadthFirstSearch(root);
        System.out.println(res);

    }

    public static List<List<Integer>> breadthFirstSearch(TreeNode root){
        List<List<Integer>> ret = new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> levelList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                levelList.add(poll.val);
                if(poll.left!=null){
                    queue.add(poll.left);
                }
                if(poll.right!=null){
                    queue.add(poll.right);
                }
            }
            ret.add(levelList);
        }
        return ret;
    }

    public static class TreeNode {
        private int val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
}
